#include <stdio.h>
int main()
{
	int p, e, i, d, flag, count, j=0;
	int result[1000];
	scanf("%d %d %d %d", &p, &e, &i, &d);
	while ( ! (p == -1 && e == -1 && i == -1 && d == -1))
	{
		p = p%23;
		e = e%28;
		i = i%33;
		flag = 1;
		count = p;
		while (flag)
		{
			count += 23;
			if ( count%23 == p && count % 28 == e && count % 33 == i && count > d)
			{
				flag = 0;
				result[j++] = ((count - d) > 21252) ? (count -d)% 21252 : count -d;
			}
		}
		scanf("%d %d %d %d", &p, &e, &i, &d);
	}
	for (i=0; i<j; i++)
	{
		printf("Case %d: the next triple peak occurs in %d days.\n", i+1, result[i]);
	}
	return 0;
}
